package net.awpspace.mobile.management.point;

import java.util.Vector;
import javax.microedition.midlet.MIDlet;
import net.awpspace.mobile.form.BaseForm;
import net.awpspace.mobile.global.Utils;
import com.sun.lwuit.Button;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.spinner.Spinner;

public class ThemDiem extends BaseForm{

	private Data data;
	
	private TextField tften,tflop,tfmasv;
	private Spinner ngay,thang,nam;
	private ComboBox tftoan,tfly,tfhoa;
	private Object[] point = new Object[]{
			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
	};
	
	public ThemDiem(MIDlet app, BaseForm parent) {
		super(app, parent);
		setTitle("Nhập điểm");
		setTransitionInAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		setTransitionOutAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		data = Data.getInstance();
//		Diem a = new Diem();
		//a.setTen("ABC");
		//a.setLop("D");
		//a.setMasv("123456");
//		data.insert(a);
		createComponent();
	}
	
	

	private void createComponent() {
		
		Container container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		
		Label lbten = new Label("Họ tên");
		tften = new TextField("");
		
		Label lblop = new Label("Học lớp");
		tflop = new TextField("");
		
		Label lbmasv = new Label("Mã Sinh viên");
		tfmasv = new TextField("");
		tfmasv.setConstraint(TextField.NUMERIC);
		tfmasv.setMaxSize(10);

		Label lbbirth = new Label("Ngày sinh");
		Container birth = new Container(new GridLayout(1, 3));
		ngay = Spinner.create(1, 32, 2, 1);
		thang = Spinner.create(1, 13, 2, 1);
		nam = Spinner.create(1940, 2014, 1991, 1);
		birth.addComponent(ngay);
		birth.addComponent(thang);
		birth.addComponent(nam);

		Container toan = new Container(new GridLayout(1, 2));
		Label lbtoan = new Label("Điểm toán");
		tftoan = new ComboBox(point);
		toan.addComponent(lbtoan);
		toan.addComponent(tftoan);
		
		Container ly = new Container(new GridLayout(1, 2));
		Label lbly = new Label("Điểm lý");
		tfly = new ComboBox(point);
		ly.addComponent(lbly);
		ly.addComponent(tfly);
		
		Container hoa = new Container(new GridLayout(1, 2));
		Label lbhoa = new Label("Điểm hóa");
		tfhoa = new ComboBox(point);
		hoa.addComponent(lbhoa);
		hoa.addComponent(tfhoa);
		
		Button savepoint = new Button(new Command("Save"));
		
		Button resetpoint = new Button(new Command("Reset"));
		
		container.addComponent(lbten);
		container.addComponent(tften);
		container.addComponent(lblop);
		container.addComponent(tflop);
		container.addComponent(lbmasv);
		container.addComponent(tfmasv);
		container.addComponent(lbbirth);
		container.addComponent(birth);
		container.addComponent(toan);
		container.addComponent(ly);
		container.addComponent(hoa);
		container.addComponent(savepoint);
		container.addComponent(resetpoint);
		this.addComponent(container);

		
	}
	public void reset() {
		tften.setText("");
		tflop.setText("");
		tfmasv.setText("");
		
	}
	
	public void save(){
		if(data == null)
			System.out.println("NULLLLLLLLLLLLLL");
		Vector list = data.getData();

		int ma;
		if (list == null)
			ma = 1;
		else
			
		ma = list.size() + 1;
		System.out.println(ma);
		
		String ten = tften.getText();
		System.out.println(ten);
		
		String lop = tflop.getText();
		System.out.println(lop);
		
		String masv = tfmasv.getText();
		System.out.println(masv);

		String ngay = String.valueOf(this.ngay.getSelectedItem());
		String thang = String.valueOf(this.thang.getSelectedItem());
		String nam = String.valueOf(this.nam.getSelectedItem());
		System.out.println(ngay + "/" + thang+ "/" + nam);
		
		int toan = Integer.parseInt(String.valueOf(tftoan.getSelectedItem()));
		System.out.println(toan);
		
		int ly = Integer.parseInt(String.valueOf(tfly.getSelectedItem()));
		System.out.println(ly);
		
		int hoa = Integer.parseInt(String.valueOf(tfhoa.getSelectedItem()));
		System.out.println(hoa);
		
		if (ten.equals("") || lop.equals("") || masv.equals("")) {
			Utils.getInstance().alert("Bạn hãy điền đầy đủ thông tin");
			return;
		}

		data.insert(new Diem(ma + "", ten, lop, masv, new Time(ngay + "/" + thang + "/" + nam), 
				toan, ly, hoa));
		Utils.getInstance().alert("Đã lưu thành công!");
	}

	protected void processCommand(ActionEvent e) {
		if(e.getCommand().getCommandName().equals("Save")){
			save();
		} else if (e.getCommand().getCommandName().equals("Reset")) {
			reset();
		}
		}
	}


